
import transaction
import cryptacular.bcrypt
from pyramid.security import Allow
from pyramid.security import Everyone
from pyramid.security import Authenticated
from sqlalchemy import Table
from sqlalchemy import Column
from sqlalchemy import ForeignKey
from sqlalchemy.orm import scoped_session
from sqlalchemy.orm import sessionmaker
from sqlalchemy.orm import relation
from sqlalchemy.orm import backref
from sqlalchemy.orm import column_property
from sqlalchemy.orm import synonym
from sqlalchemy.orm import joinedload
from sqlalchemy.types import Integer
from sqlalchemy.types import Unicode
from sqlalchemy.types import UnicodeText
from sqlalchemy.sql import func
from sqlalchemy.exc import IntegrityError
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import scoped_session
from sqlalchemy.orm import sessionmaker
from zope.sqlalchemy import ZopeTransactionExtension
DBSession = scoped_session(sessionmaker(extension=ZopeTransactionExtension()))
Base = declarative_base()

from .models import Usuario

"""
Seccion que inicializa la base de datos
"""

def initialize_sql(engine):
    DBSession.configure(bind=engine)
    Base.metadata.bind = engine
    Base.metadata.create_all(engine)
    try:
        transaction.begin()
        session = DBSession()
        usuario1 = Usuario('administrador', 'admin2012', 'Administrador', 'admin@gmail.com')
        usuario2 = Usuario('liderProyecto', 'proyecto2012', 'Lider de Proyecto', 'proyecto@gmail.com')
        usuario3 = Usuario('liderFase', 'fase2012', 'Lider de Fase', 'fase@gmail.com')
        usuario4 = Usuario('desarrollador', 'desarrollo2012', 'Desarrollador', 'desarrollo@gmail.com')

        session.add(usuario1)
        session.add(usuario2)
        session.add(usuario3)
        session.add(usuario4)
        transaction.commit()
    except IntegrityError:
        # already created
        transaction.abort()
